package com.zzyl.nursing.controller.member;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 预约信息Controller
 * 
 * @author zhengyh
 * @date 2025-03-02
 */
@Slf4j
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息的接口")
public class ReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;

    /**
     * 分页查询预约
     */
    @ApiOperation("分页查询预约")
    @GetMapping("/page")
    public R<TableDataInfo> list(@ApiParam(value = "预约信息状态") Integer status)
    {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(status);
        return R.ok(getDataTable(list));
    }

    /**
     * 取消预约
     */
    @ApiOperation("取消预约")
    @PutMapping("/{id}/cancel")
    public AjaxResult cancel(@ApiParam(value = "预约信息id") @PathVariable Long id)
    {
        reservationService.updateReservation(Reservation.builder().id(id).status(2).build());
        return AjaxResult.success();
    }

    /**
     * 查询当天取消预约数量
     */
    @ApiOperation("查询当天取消预约数量")
    @GetMapping("/cancelled-count")
    public R cancelledCount()
    {
        Integer count = reservationService.getCancelledCount();
        return R.ok(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     */
    @ApiOperation("查询每个时间段剩余预约次数")
    @GetMapping("/countByTime")
    public R<List<TimeCountVo>> countByTime(@ApiParam(value = "当前预约时间戳") Long time)
    {
        log.info("查询每个时间段剩余预约次数:{}", time);
        List<TimeCountVo> timeCountVoList = reservationService.countByTime(time);
        return R.ok(timeCountVoList);
    }

    /**
     * 新增预约
     */
    @ApiOperation("新增预约")
    @PostMapping
    public AjaxResult addReservation(@ApiParam(value = "预约信息实体类") @RequestBody Reservation reservation)
    {
        log.info("新增预约:{}", reservation);
        reservation.setStatus(0);
        reservationService.save(reservation);
        return AjaxResult.success();
    }
}
